package conf

import (
	"github.com/spf13/viper"
	"fmt"
	"github.com/whencome/xlog"
	"flag"
)

type Conf struct {
    App AppInfo `mapstructure:"app"`
	Mysql map[string]MysqlInfo `mapstructure:"mysql"`
}

type AppInfo struct {
	Name string `mapstructure:"name"`
	Port int `mapstructure:"port"`
}

type MysqlInfo struct {
	Engine string `mapstructure:"engine"`
	Host string `mapstructure:"host"`
	Port string `mapstructure:"port"`
	User string `mapstructure:"user"`
	Password string `mapstructure:"password"`
	Dbname	string `mapstructure:"dbname"`
}

var C Conf

func LoadConfig() {
	C = Conf{}

	var env string
	flag.StringVar(&env, "env", "dev", "please set env")
	flag.Parse()
	
	filename := fmt.Sprintf("./conf/%s.conf.yaml", env)
	
    viper.SetConfigType("yaml")
    viper.SetConfigFile(filename)

    err := viper.ReadInConfig()
    if err != nil {
		xlog.Errorf("read_config_failed err:%v", err)
        panic(err)
    }

    err = viper.Unmarshal(&C)
	fmt.Println(C)
    if err != nil {
		xlog.Errorf("unmarshal_config_failed err:%v", err)
		panic(err)
	}
}